//
//  LNetwork.m
//  TouchDemo
//
//  Created by 李明明 on 16/1/19.
//  Copyright © 2016年 李明明. All rights reserved.
//

#import "LNetwork.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
#import <sys/socket.h>
#import <SystemConfiguration/SCNetworkReachability.h>
@interface LNetwork ()
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) NSString *carrier;
@end

@implementation LNetwork
- (instancetype)init
{
    self = [super init];
    if (self) {
//        [self obtainStatus];
    }
    return self;
}
- (NSString *)obtainStatus {
    self.type = @"";
    struct sockaddr_storage zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.ss_len = sizeof(zeroAddress);
    zeroAddress.ss_family = AF_UNIX;
    
    SCNetworkReachabilityRef defaultRoute = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRoute, &flags);
    if (!didRetrieveFlags) {
        return @"hello world";
    }
    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) {
        self.type = @"WIFI";
    }
    if ((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0 || (flags &  kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0) {
        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) {
            self.type = @"WIFI";
        }
    }
    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN)
    {
        CTTelephonyNetworkInfo * info = [[CTTelephonyNetworkInfo alloc] init];
        NSString *currentRadioAccessTechnology = info.currentRadioAccessTechnology;
        
        if (currentRadioAccessTechnology)
        {
            if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE])
            {
                self.type =  @"4G";
            }
            else if ([currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge] || [currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])
            {
                self.type =  @"2G";
            }
            else
            {
                self.type =  @"3G";
            }
            
        }
        else
        {
            if((flags & kSCNetworkReachabilityFlagsReachable) == kSCNetworkReachabilityFlagsReachable)
            {
                if ((flags & kSCNetworkReachabilityFlagsTransientConnection) == kSCNetworkReachabilityFlagsTransientConnection)
                {
                    if((flags & kSCNetworkReachabilityFlagsConnectionRequired) == kSCNetworkReachabilityFlagsConnectionRequired)
                    {
                        self.type = @"2G";
                    }
                    else
                    {
                        self.type = @"3G";
                    }
                }
            }
        }
    }
    
    
    if ([self.type isEqualToString:@""]) {
        self.type = @"WWAN";
    }
    CTTelephonyNetworkInfo *phone = [[CTTelephonyNetworkInfo alloc] init];
    NSString *str = phone.currentRadioAccessTechnology;
//    NSString *carrier = phone.subscriberCellularProvider.carrierName;
    NSLog(@"%@\n%@", str, self.type);
    
    return self.type;
}



@end
